<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/7
 * Time: 下午4:48
 */
class Customer_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
        $this->load->model("user_model");
    }


    //查询客户和主账号表信息
    function query_customer_admin_info($customer_id)
    {
        $sql = "select * from app_customer join app_admin on app_customer.id = app_admin.customer_id WHERE app_customer.id = " . $customer_id;
        $data = $this->db->query($sql)->row_array();
        return $data;
    }

    //查询客户根据企业站客户ID
    public function query_customer_info($customer_id)
    {
        $sql = "select * from app_customer WHERE id = " . $customer_id;
        return $this->db->query($sql)->row_array();
    }

    //查询manager列表
    public function query_manager_list($customer_id)
    {
        $sql = "select * from app_customer WHERE identity = 2 and parent_id = " . $customer_id;
        return $this->db->query($sql)->result_array();
    }

    //查询客户根据商城客户ID
    public function query_shop_customer_info($shop_customer_id)
    {
        $sql = "select * from app_customer WHERE shop_user_id = " . $shop_customer_id;
        return $this->db->query($sql)->row_array();
    }

    //查询客户清单id
    public function query_customer_list_ids($customer_id)
    {
        $sql = "select list_id from app_customer_list WHERE customer_id = " . $customer_id;
        return $this->db->query($sql)->result_array();
    }

    //    manage/user限购清单
    public function query_customer_list($shop_user_id, $list_id)
    {
        $lis = array();
        foreach ($list_id as $li) {
            $lis[$li['list_id']] = $li['list_id'];
        }
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_limit_bill/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $data_arr['uid'] = $shop_user_id;
        $data_arr['tempid'] = implode(",", $lis);
        $data_arr['limit'] = 10000;
        // $data_arr['tempid'] = $lis;
//        $data_arr['tempid'] = '1728,1729';
        $lists = curl_request($url, $data_arr);
        $data = json_decode($lists, true);
        return $lists = json_decode($lists, true);
    }

    //    manage/user限购清单 cost_center
    public function query_customer_list_cost_center($shop_user_id, $list_id)
    {
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_limit_bill/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $data_arr['uid'] = $shop_user_id;
        $data_arr['tempid'] = $list_id;
        $data_arr['limit'] = 10000;
        // $data_arr['tempid'] = $lis;
//        $data_arr['tempid'] = '1728,1729';
        $lists = curl_request($url, $data_arr);
        $data = json_decode($lists, true);
        return $lists = json_decode($lists, true);
    }


    //admin 限购商品
    public function query_customer_list_product($shop_user_id)
    {
        // 商城返回过来的shop_customer_id 查询赚钱雄器具体详情
        $city_manager_base_url = $this->config->item("city_manager_base_url");
        $api_url = $city_manager_base_url . '/customer/get_customer_info_api';
        $res = curl_request($api_url, ['shop_customer_id' =>$shop_user_id]);
//        $res = curl_request($api_url, ['shop_customer_id' => 6625]);
        $res_arr = json_decode($res, true);
        if(isset($res_arr['data']['id'])){
            $data_arr['time'] = time();
            $data_arr['customer_id'] = $res_arr['data']['id'];
            $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
            $url = $this->config->item("city_manager_base_url") . '/goods/adjusted_price_goods?time=' . $data_arr['time'] . '&key=' . $data_arr['key'] . '&customer_id=' . $data_arr['customer_id'];
            $rs = curl_request($url, $data_arr);
            return $rs = json_decode($rs, true);
        }else{
            return false;
        }
    }

    // 审核信息
    public function query_app_account_period_apply($customer_id)
    {
        $sql = "select verify_status,reason,operator_id,type,created_at,updated_at from app_account_period_apply WHERE customer_id = " . $customer_id . ' order by updated_at desc';
        return $this->db->query($sql)->row_array();
    }

    // 客户 公司信息 app_company
    public function query_app_company_info($customer_id)
    {
        $sql = "select * from app_company WHERE customer_id = " . $customer_id;
        $data = $this->db->query($sql)->row_array();
        return $data;
    }

    // 保存客户基本信息
    public function save_customer_info($data, $customer_id = '')
    {
        $info['username'] = isset($data['username']) ? $data['username'] : '';
        if (!$customer_id) {
            $info['shop_user_id'] = isset($data['shop_user_id']) ? $data['shop_user_id'] : '';
        }
        if (isset($data['manager_id']) && $data['manager_id'] != '') {
            $info['manager_id'] = $data['manager_id'];
        }
        $info['company'] = isset($data['company']) ? $data['company'] : '';
        $info['name'] = isset($data['name']) ? $data['name'] : '';
        $info['tel'] = isset($data['tel']) ? $data['tel'] : '';
        $info['email'] = isset($data['email']) ? $data['email'] : '';
        $info['identity'] = isset($data['identity']) ? $data['identity'] : 3;
        $info['parent_id'] = isset($data['parent_id']) ? $data['parent_id'] : '';
        $info['cost_center_id'] = isset($data['cost_center_id']) ? $data['cost_center_id'] : '';
        $info['postage'] = isset($data['postage']) ? $data['postage'] : '';
        $info['discount'] = isset($data['discount']) ? $data['discount'] : 100;
        $info['adjustments'] = isset($data['adjustments']) ? $data['adjustments'] : '';
        $info['status'] = isset($data['status']) ? $data['status'] : '';
        $info['created_at'] = isset($data['created_at']) ? $data['created_at'] : date('Y-m-d H:i:s', time());
        if ($customer_id) {
            $re = $this->db->where(array("id" => $customer_id))->update('app_customer', $info);
        } else {
            $this->db->insert("app_customer", $info);
            $re = $this->db->insert_id();
        }
        return $re;

    }


    //保存
    public function save_app_company($customer_id, $post)
    {

        $data['name'] = isset($post['name']) ? $post['name'] : '';
        $data['province'] = isset($post['province']) ? $post['province'] : '';
        $data['city'] = isset($post['city']) ? $post['city'] : '';
        $data['district'] = isset($post['district']) ? $post['district'] : '';
        $data['address'] = isset($post['address']) ? $post['address'] : '';
        $data['business_license_pic_path'] = isset($post['business_license_pic_path']) ? $post['business_license_pic_path'] : '';
        $data['credit_code'] = isset($post['credit_code']) ? $post['credit_code'] : '';
        $data['business_license_number'] = isset($post['business_license_number']) ? $post['business_license_number'] : '';
        $data['type'] = isset($post['type']) ? $post['type'] : '';
        $data['number'] = isset($post['number']) ? $post['number'] : '';
        $data['industry'] = isset($post['industry']) ? $post['industry'] : '';
        $data['phone'] = isset($post['phone']) ? $post['phone'] : '';
        $data['contractor'] = isset($post['contractor']) ? $post['contractor'] : '';
        $data['email'] = isset($post['email']) ? $post['email'] : '';
        $data['tel'] = isset($post['tel']) ? $post['tel'] : '';
        $data['id_card_no'] = isset($post['id_card_no']) ? $post['id_card_no'] : '';
        $data['id_card_front_pic_path'] = isset($post['id_card_front_pic_path']) ? $post['id_card_front_pic_path'] : '';
        $data['id_card_back_pic_path'] = isset($post['id_card_back_pic_path']) ? $post['id_card_back_pic_path'] : '';
        $data['inviter_tel'] = isset($post['inviter_tel']) ? $post['inviter_tel'] : '';
        $data['minimum_purchase_amount'] = isset($post['minimum_purchase_amount']) ? $post['minimum_purchase_amount'] : '';
        $data['shipping_address'] = isset($post['shipping_address']) ? $post['shipping_address'] : '';
        $data['shipping_city'] = isset($post['shipping_city']) ? $post['shipping_city'] : '';
        $data['shipping_province'] = isset($post['shipping_province']) ? $post['shipping_province'] : '';
        $data['shipping_district'] = isset($post['shipping_district']) ? $post['shipping_district'] : '';
        $data['shipping_name'] = isset($post['shipping_name']) ? $post['shipping_name'] : '';
        $data['shipping_tel'] = isset($post['shipping_tel']) ? $post['shipping_tel'] : '';
        $data['shipping_name_second'] = isset($post['shipping_name_second']) ? $post['shipping_name_second'] : '';
        $data['shipping_tel_second'] = isset($post['shipping_tel_second']) ? $post['shipping_tel_second'] : '';
        $data['mark_invoice_type'] = isset($post['mark_invoice_type']) ? $post['mark_invoice_type'] : '';
        $data['invoice_type'] = isset($post['invoice_type']) ? $post['invoice_type'] : '';
        $data['invoice_company_name'] = isset($post['invoice_company_name']) ? $post['invoice_company_name'] : '';
        $data['invoice_code'] = isset($post['invoice_code']) ? $post['invoice_code'] : '';
        $data['invoice_address'] = isset($post['invoice_address']) ? $post['invoice_address'] : '';
        $data['invoice_tel'] = isset($post['invoice_tel']) ? $post['invoice_tel'] : '';
        $data['invoice_bank'] = isset($post['invoice_bank']) ? $post['invoice_bank'] : '';
        $data['invoice_account'] = isset($post['invoice_account']) ? $post['invoice_account'] : '';
        $data['invoice_normal_company_name'] = isset($post['invoice_normal_company_name']) ? $post['invoice_normal_company_name'] : '';
        $data['invoice_normal_code'] = isset($post['invoice_normal_code']) ? $post['invoice_normal_code'] : '';

        $sql = 'select id from app_company WHERE customer_id =' . $customer_id;
        $res = $this->db->query($sql)->row_array();
        if ($res) {
            $id = $res['id'];
            $bool = $this->db->where(array("id" => $id))->update('app_company', $data);
        } else {
            $data['created_at'] = date('Y-m-d H:i:s', time());
            $data['customer_id'] = $this->session->customer_id;
            $bool = $this->db->insert("app_company", $data);
        }

        return $bool;
    }

    // 企业站用户添加接口
    public function company_user_add($post_arr)
    {
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_user_add/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        if (check_param($post_arr, ['account', 'password', 'name', 'mobile', 'email'])) {
            $data_arr['account'] = $post_arr['account'];
            $data_arr['password'] = $post_arr['password'];
            $data_arr['name'] = $post_arr['name'];
            $data_arr['mobile'] = $post_arr['mobile'];
            $data_arr['email'] = $post_arr['email'];
            $data_arr['pid'] = $post_arr['parent_id'];
            if(isset($post_arr['type']) && $post_arr['type']==""){
                $data_arr['type'] = 0;
            }else{
                $data_arr['type'] = $post_arr['type'];
            }
            $rs = curl_request($url, $data_arr);
            $rs = json_decode($rs, true);
            return $rs;
        } else {
            return false;
        }
    }

    //用户修改接口
    public function company_user_edit($post_arr)
    {

        $url = $this->config->item("shop_url") . 'companymanagerapi/company_user_edit/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $data_arr['uid'] = $post_arr['shop_user_id'];
        if (isset($post_arr['tel'])) {
            $data_arr['mobile'] = $post_arr['tel'];
        }
        if (isset($post_arr['name'])) {
            $data_arr['name'] = $post_arr['name'];
        }
        if (isset($post_arr['email'])) {
            $data_arr['email'] = $post_arr['email'];
        }
        if (!empty($post_arr['password'])) {
            $data_arr['password'] = $post_arr['password'];
        }
        $rs = curl_request($url, $data_arr);
        $rs = json_decode($rs, true);
        return $rs;
//        if ($rs['code'] == 200) {
//            return $rs['data']['shop_user_id'];
//        } else {
//            return false;
//        }
    }

    //获取admin  d
    public function get_admin_customer_id($customer_id)
    {
        $where = array('id' => $customer_id);
        $row1 = $this->db->select("id,identity,parent_id")->where($where)->get('app_customer')->row_array();
        if ($row1['identity'] == 1) {
            return $row1['id'];
        }
        $where = array('id' => $row1['parent_id']);
        $row2 = $this->db->select("id,identity,parent_id")->where($where)->get('app_customer')->row_array();
        if ($row2['identity'] == 1) {
            return $row2['id'];
        }
        $where = array('id' => $row2['parent_id']);
        $row3 = $this->db->select("id,identity,parent_id")->where($where)->get('app_customer')->row_array();
        if ($row3['identity'] == 1) {
            return $row3['id'];
        }
        return false;
    }

    //获取上级用户的信息
    public function get_parent_info($customer_id)
    {
        $where = array('id' => $customer_id);
        $row = $this->db->where($where)->select('parent_id')->from('app_customer')->get()->row_array();
        $where = array('id' => $row['parent_id']);
        $info = $this->db->where($where)->from('app_customer')->get()->row_array();
        return $info;
    }

    //获取admin 和 cuntomer  id
    public function get_admin_customer($customer_id)
    {
        $where = array('id' => $customer_id);
        $row1 = $this->db->select("parent_id")->where($where)->get('app_customer')->row_array();
        $where = array('id' => $row1['parent_id']);
        $row2 = $this->db->select("parent_id,username,id,email")->where($where)->get('app_customer')->row_array();
        $user[] = $row2;
        if ($row2['parent_id'] != 0) {
            $where = array('id' => $row2['parent_id']);
            $row3 = $this->db->select("parent_id,username,id,email")->where($where)->get('app_customer')->row_array();
            $user[] = $row3;
        }
        return $user;
    }


    //获取登錄用戶下面的子辈或孙辈的(包含自己)
    //type=1 存在则不包含自己
    public function get_customer_user($customer_id,$type="")
    {
        $where = array('parent_id' => $customer_id);
        $member = $this->db->from('app_customer')->select("id")->where($where)->get()->result_array();
        $str[] = $customer_id;
        foreach ($member as $k => $v) {
            $str[] = $v['id'];
        }

        foreach ($member as $k => $v) {
            $where = array('parent_id' => $v['id']);
            $row = $this->db->from('app_customer')->select("id")->where($where)->get()->result_array();
            foreach ($row as $key => $val) {
                $str[] = $val['id'];
            }
        }
        if($type==1){
            foreach ($str as $key => $value) {
                if($value == $customer_id){
                    unset($str[$key]);
                }

            }
        }
        if(count($str) == ""){
            $str[] = "-1";
        }
        return $str;
    }

    //获取admin下面的子辈和孙辈的(不包含自己)
    public function get_customer_user_parent($customer_id)
    {
        $where = array('parent_id' => $customer_id);
        $member = $this->db->from('app_customer')->select("id")->where($where)->get()->result_array();
        $str1 = array();
        foreach ($member as $k => $v) {
            $str1[] = $v['id'];
        }
        $str2 = array();
        foreach ($member as $k => $v) {
            $where = array('parent_id' => $v['id']);
            $row = $this->db->from('app_customer')->select("id")->where($where)->get()->result_array();
            foreach ($row as $key => $val) {
                $str2[] = $val['id'];
            }
        }
        $data['str1'] = $str1;
        $data['str2'] = $str2;
        return $data;

    }

    //获取U-M每月所剩的账期额度和admin总共所剩的账期额度
    public function member_total_quota_left_month($customer_id, $identity)
    {
        if ($identity != 1) {
            $where = array('customer_id' => $customer_id);
            $manager_user_account = $this->db->where($where)->select('total_quota')->get('app_manager_user')->row_array();
            $where = array('customer_id' => $customer_id, 'month' => date('Y-m', time()));
            $manager_user_account_used = $this->db->where($where)->select('used,budget')->get('app_budget')->row_array();
            $data['total_quota_left'] = $manager_user_account['total_quota'] - $manager_user_account_used['used'];
        } else {
            $where = array(
                'customer_id' => $customer_id
            );
            $admin_account = $this->db->where($where)->select('account_period_quota,account_period_quota_used')->get('app_admin')->row_array();
            $data['total_quota_left'] = $admin_account['account_period_quota'] - $admin_account['account_period_quota_used'];
        }
        return $data['total_quota_left'];
    }

    //获取U-M单品商品额度,和每月额度
    public function u_m_single_quota($customer_id)
    {
        $where = array(
            'customer_id' => $customer_id
        );
        //获取U-M用户单品额度
        $user = $this->db->where($where)->select('total_quota,single_quota')->get('app_manager_user')->row_array();
        if ($user) {
            $data['total_quota'] = $user['total_quota'];
            $data['single_quota'] = $user['single_quota'];
            return $data;
        } else {
            return false;
        }

    }

    //获取U-M单品商品额度
    public function u_m_single_quota_q($customer_id)
    {
        $where = array(
            'customer_id' => $customer_id
        );
        //获取U-M用户单品额度
        $user = $this->db->where($where)->select('single_quota')->get('app_manager_user')->row_array();
        if ($user) {
            return $user['single_quota'];
        } else {
            return false;
        }

    }

    //获取U-M用户清单id
    public function u_m_list_ids($customer_id)
    {
        $where = array(
            'customer_id' => $customer_id
        );
        $list = $this->db->where($where)->select('list_id')->get('app_customer_list')->result_array();
        if ($list) {
            $data['list_id'] = [];
            foreach ($list as $value) {
                $data['list_id'][] = $value['list_id'];
            }
            return $data['list_id'];
        } else {
            return false;
        }
    }

    //获取U-M用户清单里的商品id
    public function u_m_list_goods_ids($customer_id)
    {
        $where = array(
            'customer_id' => $customer_id
        );
        $list = $this->db->where($where)->select('list_id')->get('app_customer_list')->result_array();
        if ($list) {
            $dataArr['tempid'] = "";
            foreach ($list as $value) {
                $dataArr['tempid'] .= ',' . $value['list_id'];
            }
            $dataArr['uid'] = $this->get_admin_customer_id($customer_id);

            $url = $this->config->item("shop_url") . 'companymanagerapi/company_limit_bill/';
            $dataArr['time'] = time();
            $dataArr['key'] = md5($this->config->item("token_const_wjyh") . $dataArr['time']);
            $dataArr['tempid'] = mb_substr($dataArr['tempid'], 1, strlen($dataArr['tempid']));
            $dataArr['uid'] = $this->user_model->customer_id_to_shop_user_id($dataArr['uid']);


            $rs1 = curl_request($url, $dataArr);
            $rs = json_decode($rs1, true);
            if ($rs['code'] == 200) {
                $data['list_id'] = [];
                foreach ($rs['data'] as $value) {
                    $data['list_id'][] = $value['goods_id'];
                }
                return $data['list_id'];
            }
        } else {
            return false;
        }
    }

    //获取admin成本中心部门
    public function get_admin_cost_center($customer_id)
    {
        $where = array(
            'customer_id' => $customer_id
        );
        $cost_center = $this->db->where($where)->from('app_cost_center')->get()->result_array();
        if ($cost_center) {
            return $cost_center;
        } else {
            return false;
        }
    }

}